package com.chuangshi.core.vote.position.service.impl;

import com.chuangshi.core.kit.Result;
import com.chuangshi.core.kit.Column;
import com.chuangshi.core.mongodb.MongodbServiceImpl;
import com.chuangshi.core.vote.position.controller.admin.request.PositionAdminV1ListAllRequest;
import com.chuangshi.core.vote.position.controller.admin.request.PositionAdminV1ListRequest;
import com.chuangshi.core.vote.position.entity.Position;
import com.chuangshi.core.vote.position.entity.column.PositionColumn;
import com.chuangshi.core.vote.position.service.PositionService;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 岗位信息业务接口实现
 *
 * @author ggliming
 * @date 2019-02-21
 */
@Service
public class PositionServiceImpl extends MongodbServiceImpl<Position> implements PositionService {

    @Override
    public Result listForAdmin(PositionAdminV1ListRequest request) {
        Criteria criteria = Criteria.where(Column.APP_ID).is(request.getAppId())
                        .and(Column.SYSTEM_STATUS).is(true);
        if (StrUtil.isNotBlank(request.getPositionId())) {
            criteria.and(PositionColumn.POSITION_ID).regex(".*?" + request.getPositionId() + ".*");
        }
        if (StrUtil.isNotBlank(request.getPositionName())) {
            criteria.and(PositionColumn.POSITION_NAME).regex(".*?" + request.getPositionName() + ".*");
        }

        List<Sort.Order> orders = new ArrayList<Sort.Order>();
        orders.add(new Sort.Order(Sort.Direction.ASC, PositionColumn.POSITION_SORT));
        orders.add(new Sort.Order(Sort.Direction.DESC, PositionColumn.POSITION_ID));
        Sort sort = Sort.by(orders);

        Integer total = count(criteria);
        List<Position> list = list(criteria, sort, request.getPageIndex(), request.getPageSize());


        Result result = new Result();
        result.setTotal(total);
        result.setList(list);

        return result;
    }

    @Override
    public List<Position> listAll(String appId) {
        Criteria criteria = Criteria.where(Column.APP_ID).is(appId)
                .and(Column.SYSTEM_STATUS).is(true);

        List<Sort.Order> orders = new ArrayList<Sort.Order>();
        orders.add(new Sort.Order(Sort.Direction.ASC, PositionColumn.POSITION_SORT));
        orders.add(new Sort.Order(Sort.Direction.DESC, PositionColumn.POSITION_ID));
        Sort sort = Sort.by(orders);

        return list(criteria, sort);
    }
}